<?php
/**
 * Created by PhpStorm.
 * User: mhx
 * 处理excel文件,要将excel文件转换成.csv格式
 *文件存放目录 /news/doc/
 */


class phpexcelController extends baseController{

    public function __construct()
    {
        parent::__construct();
    }

    public function run()
    {
        /*
Array
(
    [0] => 博物馆名称
    [1] => 博物馆性质
    [2] => 质量等级
    [3] => 地址
)
Array
(
    [0] => 北京市（131家）
    [1] =>
    [2] =>
    [3] =>
)
Array
(
    [0] => 故宫博物院
    [1] => 国有
    [2] => 一级
    [3] => 东城区景山前街4号
)
         *
         */
        $map = new mapController();
        $cityid = new commonController();
        /*$position = $map->test('故宫博物院');
        echo 'test:::';
        print_r($position);
        echo '<br />','geocode:::';
        $position = $map->geocode('故宫博物院');
        print_r($position);
        die;*/
        $abc = 0;
        echo '第一篇';
        $city = '';
        $insert = array();
        $filename = ROOT_PATH . 'doc/2012.csv';
        $fp = fopen($filename, 'rb');
        //$i = 0;
        while (!feof($fp)) {
            $data = fgetcsv($fp);
            echo $abc++;
            //print_r($data);
            //continue;
            $insert = array();
            if ($data[1] == ''&&$data[2] == ''&&$data[3]==''){
                $city = $data[0];
                $pos = mb_strpos($city,'（');
                if($pos !== false){
                    $city = mb_substr($city,0,$pos);
                }else{
                    $city = $data[0];
                }

            }else{
                $insert['apellation'] =str_replace('"','',$data[0]);
                if($insert['apellation'] == '博物馆名称'){continue;}
                $findbyname=model('companies')->find("apellation='".$insert['apellation']."'");
                if($findbyname){continue;};
                //$insert['province'] = $city;
                $insert['address'] = $data[3];
                $position = $map->getlocation($insert['apellation'],$city);
                if($position['longitude'] == '') {
                    $position = $map->getlocation($insert['address'],$city);
                }
                if($position['longitude'] != '') {
                    $insert['longitude'] = $position['longitude'];
                    $insert['latitude'] = $position['latitude'];
                    $insertcity = $cityid->getcityid($position['city']);

                    $insert['city'] = $insertcity['id'];
                }
                /*++$i;
                if($i  == 5){
                    die;
                }
                print_r($insert);*/

                if ($insert['longitude'] == '') {
                    print_r($insert);
                }
                model('companies')->insert($insert);


            }
                /*echo '有一条:';
                print_r($data);*/
            }
        //fgetcsv($fp);
        fclose($fp);


        /*
         * Array
(
    [0] =>
    [1] => 博物馆名称
    [2] => 博物馆性质
    [3] => 质量等级
    [4] => 是否免费开放
    [5] => 地址
    [6] =>
)
Array
(
    [0] =>
    [1] => 北京市（151家）
    [2] =>
    [3] =>
    [4] =>
    [5] =>
    [6] =>
)
Array
(
    [0] =>
    [1] => 故宫博物院
    [2] => 文物
    [3] => 一级
    [4] => 否
    [5] => 东城区景山前街4号
    [6] =>
)
         *
         *
         */
        echo '第2篇';

        $filename = ROOT_PATH . 'doc/2013.csv';
        $fp = fopen($filename, 'rb');
        while (!feof($fp)) {
            $data = fgetcsv($fp);

            echo $abc++;

            $insert = array();
            if ($data[0] == ''&&$data[2] == ''&&$data[3]==''&&$data[4]==''&&$data[5]==''){
                $city = $data[1];
                $pos = mb_strpos($city,'（');
                if($pos !== false){
                    $city = mb_substr($city,0,$pos);
                }else{
                    $city = $data[1];
                }
            }else{
                $insert['apellation'] = $data[1];

                if($insert['apellation'] == '博物馆名称'){continue;}
                $findbyname=model('companies')->find("apellation='".$insert['apellation']."'");
                if($findbyname){continue;};

                //$insert['province'] = $city;
                $insert['address'] = $data[5];
                $position = $map->getlocation($insert['apellation'],$city);
                if($position['longitude'] == '') {
                    $position = $map->getlocation($insert['address'],$city);
                }
                if($position['longitude'] != '') {
                    $insert['longitude'] = $position['longitude'];
                    $insert['latitude'] = $position['latitude'];
                    $insertcity = $cityid->getcityid($position['city']);

                    $insert['city'] = $insertcity['id'];

                }
                $findbyname=model('companies')->find("apellation='".$insert['apellation']."'");
                if(!$findbyname) {
                    model('companies')->insert($insert);
                }
            }

            /*echo '有一条:';
            print_r($data);*/
        }
        //fgetcsv($fp);
        fclose($fp);


/*
Array
(
    [0] => 博物馆名称
    [1] => 博物馆性质
    [2] =>
    [3] => 质量等级
    [4] => 地址
)
Array
(
    [0] => 北京市（146家）
    [1] =>
    [2] =>
    [3] =>
    [4] =>
)
Array
(
    [0] => 故宫博物院
    [1] => 文物
    [2] =>
    [3] => 一级
    [4] => 东城区景山前街4号
)
         *
         */
        echo '第3篇';

        $filename = ROOT_PATH . 'doc/2014.csv';
        $fp = fopen($filename, 'rb');
        while (!feof($fp)) {

            echo $abc++;

            $data = fgetcsv($fp);
            $insert = array();
            if ($data[1] == ''&&$data[2] == ''&&$data[3]==''&&$data[4]==''){
                $city = $data[0];
                $pos = mb_strpos($city,'（');
                if($pos !== false){
                    $city = mb_substr($city,0,$pos);
                }else{
                    $city = $data[0];
                }
            }else{
                $insert['apellation'] = $data[0];

                if($insert['apellation'] == '博物馆名称'){continue;}
                $findbyname=model('companies')->find("apellation='".$insert['apellation']."'");
                if($findbyname){continue;};

                //$insert['province'] = $city;
                $insert['address'] = $data[4];
                $position = $map->getlocation($insert['apellation'],$city);
                if($position['longitude'] == '') {
                    $position = $map->getlocation($insert['address'],$city);
                }
                if($position['longitude'] != '') {
                    $insert['longitude'] = $position['longitude'];
                    $insert['latitude'] = $position['latitude'];
                    $insertcity = $cityid->getcityid($position['city']);

                    $insert['city'] = $insertcity['id'];

                }
                $findbyname=model('companies')->find("apellation='".$insert['apellation']."'");
                if(!$findbyname) {
                    model('companies')->insert($insert);
                }
            }

            //if ($data[4]){}
            /*echo '有一条:';
            print_r($data);*/
        }
        //fgetcsv($fp);
        fclose($fp);

        $museum = new museumMakerController();
        $museum->makedetailed();

        return null;

    }


}